package org.example.medicalstaff.controller;


import com.alibaba.fastjson.JSONObject;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.http.HttpRequest;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.example.medicalstaff.entity.CheckApply;
import org.example.medicalstaff.entity.MedicalRecord;
import org.example.medicalstaff.entity.Register;
import org.example.medicalstaff.service.PatientClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/out_doctor")
public class PatientClientController {

    @Autowired
    private PatientClientService patientClientService;

    /**
     * API1 通过查询Register表获取患者信息
     * 所给url格式如下：http://localhost:9090/out_doctor/patient/all/1
     * 备注：这个user_id是医生的user_id不是病人的ID
     * 测试结果：正常
     */
    @GetMapping("/patient/all/{user_id}")
    public CommonResponse<List<Register>> getAllPatient(
            @PathVariable("user_id") int userID
    ){
        return patientClientService.getAllPatient(userID);
    }

    /**
     * API2 提交患者病历首页信息
     * 所给url格式如下：http://localhost:9090/out_doctor/patient/submit?CaseNumber=6006192345&RegistID=357&Readme=aa&Present=bb&PresentTreat=aa&History=aa&Allergy=aa&Physique=aa&Proposal=aa&Careful=aa
     * 备注：默认结果：0或null
     * 测试结果：正常
     */
    @PostMapping("/patient/submit")
    public CommonResponse<String> submit_info(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        String CaseNumber = (String)jsonObject.get("CaseNumber");
        int RegistID = (int) jsonObject.get("RegistID");
        String Readme = (String)jsonObject.get("Readme");
        String Present = (String)jsonObject.get("Present");
        String PresentTreat = (String)jsonObject.get("PresentTreat");
        String History = (String)jsonObject.get("History");
        String Allergy = (String)jsonObject.get("Allergy");
        String Physique = (String)jsonObject.get("Physique");
        String Proposal = (String)jsonObject.get("Proposal");
        String Careful = (String)jsonObject.get("Careful");
        CommonResponse<String> commonResponse = patientClientService.submit_info(
                CaseNumber,
                RegistID,
                Readme,
                Present,
                PresentTreat,
                History,
                Allergy,
                Physique,
                Proposal,
                Careful
        );
        return commonResponse;
    }

    /**
     * API3 重新加载患者病历首页信息
     * 所给url格式如下：http://localhost:9090/out_doctor/patient/refresh?CaseNumber=600608&RegistID=27
     * 备注：这个CaseNumber和RegistID要对应不然查不到数据
     * 测试结果：正常
     */
    @GetMapping("/patient/refresh")
    public CommonResponse<List<MedicalRecord>> refresh(
            @RequestParam("CaseNumber") String CaseNumber,
            @RequestParam("RegistID") int RegistID
    ){
        return patientClientService.refresh(CaseNumber,RegistID);
    }

    /**
     * API4 提交初步诊断结果
     * 所给url格式如下：http://localhost:9090/out_doctor/patient/submit_first?MedicalID=33&RegistID=27&DiseaseID=56&DiagnoseType=1&GetSiskDate=2019-04-01 13:26:36&DiagnoseCate=1
     * 备注：插入数据库的操作没有主外键约束，所以前端同学自己要判断一下所给值是否合理
     * 测试结果：正常
     */
    @PostMapping("/patient/submit_first")
    public CommonResponse<String> submit_diagnosis(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int DiseaseID = Integer.parseInt((String) jsonObject.get("DiseaseID"));
        int DiagnoseType = Integer.parseInt((String) jsonObject.get("DiagnoseType"));
        CommonResponse<String> commonResponse = patientClientService.submit_diagnosis(
                MedicalID,
                RegistID,
                DiseaseID,
                DiagnoseType
        );
        return commonResponse;
    }

    /**
     * API5 门诊医生查看项目检查结果
     * url格式如下：http://localhost:9090/out_doctor/patient/view_result?RegistID=9&MedicalID=5&ItemID=25
     * 备注：同API3
     * 测试结果：正常
     */
    @GetMapping("/patient/view_result")
    public CommonResponse<CheckApply> look_check_pro(
            @RequestParam("RegistID") int RegistID,
            @RequestParam("MedicalID") int MedicalID,
            @RequestParam("ItemID") int ItemID
    ){
        //根据这两个属性从checkApply表中获取其他数据，返回6条。并且状态要修改为5。那我不如把整个对象返回去，让前端去取其中的值。
        CommonResponse<CheckApply> commonResponse = patientClientService.look_check_pro(
                RegistID,
                MedicalID,
                ItemID
        );
        return commonResponse;
    }

    /**
     * API6 提交最终确诊结果
     * url格式如下：http://localhost:9090/out_doctor/patient/submit_end?RegistID=27&MedicalID=8&Diagnosis=有病&Handling=去治&DiseaseID=9&DiagnoseCate=2
     * 备注：这个主要修改MedicalID和RegistID，注意前两个参数不能给错，不然会找不到，然后后面的ID属性的数据库没有设置主外键约束，所以需要前端同学注意
     * 测试结果：正常
     */
    @PutMapping("/patient/submit_end")
    public CommonResponse<String> final_diagnosis(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        String Diagnosis = (String)jsonObject.get("Diagnosis");
        String Handling = (String)jsonObject.get("Handling");
        int DiagnoseCate = (int) jsonObject.get("DiagnoseCate");
        //我理解的：首先通过MedicalID和RegistID来定位MedicalDisease中的这条记录，然后修改剩下四个属性，属性值来自所给参数；
        //再通过MedicalID和RegistID来定位MedicalRecord中的记录，然后修改剩下两个属性，属性值来自所给参数。
        CommonResponse<String> commonResponse = patientClientService.final_diagnosis(
                MedicalID,
                RegistID,
                Diagnosis,
                Handling,
                DiagnoseCate
        );
        return commonResponse;
    }

    /**
     * API7 结束门诊——诊毕
     * url格式如下：http://localhost:9090/out_doctor/patient/end_service?RegistID=357&MedicalID=27
     * 备注：无
     * 测试结果：正常
     */
    @PutMapping("/patient/end_service")
    public CommonResponse<String> end_service(
            @RequestParam("MedicalID") int MedicalID,
            @RequestParam("RegistID") int RegistID
    ){
        return patientClientService.end_service(MedicalID,RegistID);
    }

}
